package org.zcode.generator.core.generator.method;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.zcode.generator.core.generator.MethodCreator;
import org.zcode.generator.core.util.GeneratorUtil;
import org.zcode.generator.core.config.BeanTypeConfig;
import org.zcode.generator.core.config.XmlConfig;

/**
 * @Description:
 * @author zhoulk
 * @date: 2019/10/15
 */
public class DataSourceMethodCreator implements MethodCreator {

    private XmlConfig xmlConfig;
    private BeanTypeConfig beanTypeConfig;
    private IntrospectedTable introspectedTable;
    private String tableRemarks;

    public DataSourceMethodCreator(XmlConfig xmlConfig,
                                   BeanTypeConfig beanTypeConfig,
                                   IntrospectedTable introspectedTable) {
        this.xmlConfig = xmlConfig;
        this.beanTypeConfig = beanTypeConfig;
        this.introspectedTable = introspectedTable;
        this.tableRemarks = GeneratorUtil.getTableRemarks(introspectedTable.getRemarks());
    }

    @Override
    public Method qryByIdMethod(String tableName, int flag) {
        Method method = new Method();
        method.setName("qry" + tableName + "ById");
        method.setReturnType(beanTypeConfig.pojoType);
        for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
            FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
            method.addParameter(new Parameter(type, introspectedColumn.getJavaProperty()));
        }
        if (flag == 1) {
            method.addJavaDocLine(GeneratorUtil.getDocTmpl(xmlConfig.servicePack + ".I" + tableName + "Service.qry" + tableName + "ById",
                    method.getParameters().get(0).getName(),
                    beanTypeConfig.pojoType.getShortName(),
                    "根据主键查询", xmlConfig.author));
        }
        if (flag == 0) {
            method.setVisibility(JavaVisibility.PUBLIC);
        }
        StringBuilder sb = new StringBuilder();
        sb.append("return ");
        sb.append(getDaoShort());
        sb.append(introspectedTable.getSelectByPrimaryKeyStatementId());
        sb.append("(");
        for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
            sb.append(introspectedColumn.getJavaProperty());
            sb.append(",");
        }
        sb.setLength(sb.length() - 1);
        sb.append(");");
        method.addBodyLine(sb.toString());
        return method;
    }

    @Override
    public Method countMethod(String tableName, int flag) {
        Method method = new Method();
        method.setName("count" + tableName);
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.addParameter(new Parameter(beanTypeConfig.pojoConditionType, "condition"));
        if (flag == 1) {
            method.addJavaDocLine(
                    GeneratorUtil.getDocTmpl(xmlConfig.servicePack + ".I" + tableName + "Service.count" + tableName,
                            method.getParameters().get(0).getName(),
                            FullyQualifiedJavaType.getIntInstance().getShortName(),
                            tableRemarks + "数量查询接口", xmlConfig.author));
        }
        if (flag == 0) {
            method.setVisibility(JavaVisibility.PUBLIC);
        }

        StringBuilder sb = new StringBuilder();
        sb.append("return ");
        sb.append(getDaoShort());
        sb.append(introspectedTable.getCountByExampleStatementId());
        sb.append("(");
        sb.append("condition");
        sb.append(");");
        method.addBodyLine(sb.toString());
        return method;
    }

    @Override
    public Method qrysMethod(String tableName, int flag) {
        Method method = new Method();
        method.setName("qry" + tableName + "s");
        method.setReturnType(new FullyQualifiedJavaType("List<" + beanTypeConfig.pojoType.getShortName() + ">"));
        method.addParameter(new Parameter(beanTypeConfig.pojoConditionType, "condition"));
        if (flag == 1) {
            method.addJavaDocLine(GeneratorUtil.getDocTmpl(xmlConfig.servicePack + ".I" + tableName + "Service.qry" + tableName + "s", method.getParameters().get(0).getName(),
                    new FullyQualifiedJavaType("List<" + beanTypeConfig.pojoType.getShortName() + ">").getShortName(), tableRemarks + "列表查询接口", xmlConfig.author));
        }
        if (flag == 0) {
            method.setVisibility(JavaVisibility.PUBLIC);
        }
        StringBuilder sb = new StringBuilder();
        sb.append("condition.setDataState(CommonConst.DataStates.NORMAL_STATE);");
        method.addBodyLine(sb.toString());
        sb.setLength(0);
        sb.append("return ");
        sb.append(getDaoShort());
        sb.append(introspectedTable.getSelectByExampleStatementId());
        sb.append("(");
        sb.append("condition");
        sb.append(");");
        method.addBodyLine(sb.toString());
        return method;
    }

    @Override
    public Method updMethod(String tableName, int flag) {
        Method method = new Method();
        method.setName("upd" + tableName + "ById");
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.addParameter(new Parameter(beanTypeConfig.pojoType, "inParam"));
        if (flag == 1) {
            method.addJavaDocLine(GeneratorUtil.getDocTmpl(xmlConfig.servicePack + ".I" + tableName + "Service.upd" + tableName + "ById",
                    method.getParameters().get(0).getName(), FullyQualifiedJavaType.getIntInstance().getShortName(),
                    tableRemarks + "根据Id更新接口", xmlConfig.author));
        }
        if (flag == 0) {
            method.setVisibility(JavaVisibility.PUBLIC);
        }
        StringBuilder sb = new StringBuilder();
        sb.append("return ");
        sb.append(getDaoShort());
        sb.append(introspectedTable.getUpdateByPrimaryKeySelectiveStatementId());
        sb.append("(");
        sb.append("inParam");
        sb.append(");");
        method.addBodyLine(sb.toString());
        return method;
    }

    @Override
    public Method addMethod(String tableName, int flag) {
        Method method = new Method();
        method.setName("add" + tableName);
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.addParameter(new Parameter(beanTypeConfig.pojoType, "inParam"));
        method.setVisibility(JavaVisibility.PUBLIC);
        if (flag == 1) {
            method.addJavaDocLine(GeneratorUtil.getDocTmpl(xmlConfig.servicePack + ".I" + tableName + "Service.add" + tableName,
                    method.getParameters().get(0).getName(), FullyQualifiedJavaType.getIntInstance().getShortName(),
                    tableRemarks + "新增接口", xmlConfig.author));
        }
        if (flag == 0) {
            method.setVisibility(JavaVisibility.PUBLIC);
        }
        StringBuilder sb = new StringBuilder();
        sb.append(getDaoShort());
        sb.append(introspectedTable.getInsertStatementId());
        sb.append("(");
        sb.append("inParam");
        sb.append(");");
        method.addBodyLine(sb.toString());
        return method;
    }

    private String getDaoShort() {
        return GeneratorUtil.toLowerCase(beanTypeConfig.daoType.getShortName()) + ".";
    }
}
